﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using Spring.Data.NHibernate.Generic;
using Spring.Data.NHibernate.Generic.Support;
using NHibernate.Linq;
using EIIS.FrameWork.Repository;
using EIIS.FrameWork.DomainModel;

namespace EIIS.Infrastructure.Repositories
{
    public abstract class RepositoryBase<T> : HibernateDaoSupport, IRepository<T> where T : Entity
    {
        public virtual object Save(T entity)
        {
            return this.HibernateTemplate.Save(entity);
        }

        public virtual T Get(object id)
        {
            return this.HibernateTemplate.Get<T>(id);
        }

        public virtual T Load(object id)
        {
            return this.HibernateTemplate.Load<T>(id);
        }

        public virtual IQueryable<T> LoadAll()
        {
            var result = Session.Query<T>();
            return result;
        }

        public virtual void Update(T entity)
        {
            this.HibernateTemplate.Update(entity);
        }

        public virtual void Delete(object id)
        {
            var entity = this.HibernateTemplate.Get<T>(id);
            if (entity == null)
            {
                return;
            }

            this.HibernateTemplate.Delete(entity);
        }

        public virtual IQueryable<T> LoadAllWithPage(out long count, int pageIndex, int pageSize)
        {
            var result = Session.Query<T>();
            count = result.LongCount();
            return result.Skip((pageIndex - 1) * pageSize).Take(pageSize);
        }


        public virtual void Delete(IList<object> idList)
        {
            foreach (var item in idList)
            {
                var entity = this.HibernateTemplate.Get<T>(item);
                if (entity == null)
                {
                    return;
                }
                this.HibernateTemplate.Delete(entity);
            }
        }

        public virtual void SaveOrUpdate(T entity)
        {
            this.HibernateTemplate.SaveOrUpdate(entity);
        }

        public virtual IList<T> Find(string strQueryString)
        {
            return this.HibernateTemplate.Find<T>(strQueryString);
        }

        public virtual object ExecuteSQL(string strSql)
        {
          return  Session.CreateSQLQuery(strSql).ExecuteUpdate();
        }

        public virtual IList ExecuteSelectSQL(string strSql)
        {
            return Session.CreateSQLQuery(strSql).List();
        }

        public virtual IList<T> ExecuteHQL(string strQueryString)
        {
            return Session.CreateQuery(strQueryString).List<T>();
        }

        /// <summary>
        /// 判断字段的值是否存在
        /// </summary>
        /// <param name="fieldName">字段名称</param>
        /// <param name="fieldValue">字段值</param>
        /// <param name="id">Id的值，新增为-1，修改为实际值</param>
        /// <param name="where">其他的查询条件</param>
        /// <returns></returns>
        public virtual bool IsFieldExist(string fieldName, string fieldValue, string id, string where)
        {
            if (!string.IsNullOrEmpty(where))
                where = @" and " + where;
            var query = Session.CreateQuery(
                string.Format(@"select count(*) from {0} as o where o.{1}='{2}' and o.Id<>'{3}'" + where,
                typeof(T).Name,
                fieldName,
                fieldValue, id));

            return query.UniqueResult<long>() > 0;
        }
    }
}
